﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;

namespace ImageResizer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void runButton_Click(object sender, EventArgs e)
        {
            byte[] imageBytes = this.GetImageBytes();

            byte[] shrunkImage = this.ShrinkImage(imageBytes, 100);

            this.SaveImageBytes(shrunkImage);
        }

        private byte[] GetImageBytes()
        {
            return File.ReadAllBytes("bigImage.jpg");
        }

        private void SaveImageBytes(byte[] shrunkImage)
        {
            File.WriteAllBytes("smallImage.jpg", shrunkImage);
        }

        private byte[] ShrinkImage(byte[] imageBytes, int height)
        {
            Bitmap image = new Bitmap(new MemoryStream(imageBytes));

            int width = (height) * (image.Width / image.Height);

            Image shrunkImage = image.GetThumbnailImage(width, height, null, IntPtr.Zero);

            MemoryStream output = new MemoryStream();

            shrunkImage.Save(output, ImageFormat.Jpeg);

            return output.ToArray();
        }
    }
}
